﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pibby.Core
{
	public class Duration
	{
		public Duration()
		{
		}

		public Duration(string text)
			: this()
		{
			try
			{
				if (!string.IsNullOrEmpty(text))
				{
					const char colon = ':';

					string[] words = text.Split(colon);

					if (words.Length >= 3)
					{
						this.Hours = Convert.ToInt32(words[0]);
						this.Minutes = Convert.ToInt32(words[1]);
						this.Seconds = Convert.ToInt32(words[2]);
					}
					else if (words.Length == 2)
					{
						this.Minutes = Convert.ToInt32(words[0]);
						this.Seconds = Convert.ToInt32(words[1]);
					}
					else if (words.Length == 1)
					{
						int seconds = Convert.ToInt32(words[0]);

						var ts = new TimeSpan(0, 0, seconds);

						this.Hours = ts.Hours;
						this.Minutes = ts.Minutes;
						this.Seconds = ts.Seconds;
					}
				}
			}
			catch (FormatException)
			{
				this.Hours = this.Minutes = this.Seconds = 0;
			}
		}

		public override string ToString()
		{
			 return this.AsText;
		}

		public string AsText
		{
			get
			{
				return string.Format(
                                System.Globalization.CultureInfo.CurrentUICulture,
								"{0:00}:{1:00}:{2:00}", 
                                this.Hours, 
                                this.Minutes, 
                                this.Seconds);
			}
		}

		public int Hours { get; private set; }

		public int Minutes { get; private set; }
		
		public int Seconds { get; private set; }

		public int Value
		{
			get
			{
				return this.Seconds + (60 * this.Minutes) + (60 * 60 * this.Hours);
			}
		}
	}
}
